package com.example.business.service;

import cn.hutool.core.bean.BeanUtil;
import com.example.common.response.PageResponse;
import com.example.business.entity.Product;
import com.example.business.entity.ProductExample;
import com.example.business.mapper.ProductMapper;
import com.example.business.request.ProductListRequest;
import com.example.business.response.ProductResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.example.common.util.SnowflakeUtil;
import com.example.business.request.ProductRequest;

@Service
public class ProductService {
    private static final Logger log = LoggerFactory.getLogger(ProductService.class);
    @Autowired
    ProductMapper productMapper;
    @Autowired
    private ProductSkuService productSkuService;

    public PageResponse<ProductResponse> list(ProductListRequest request) {
        ProductExample productExample = new ProductExample();
        // 按 id 字段逆序排序
        productExample.setOrderByClause("id desc");

        // 查询条件
        ProductExample.Criteria criteria = productExample.createCriteria();

        // 增加查询条件：关键词搜索
        if (request.getKeyword() != null && !request.getKeyword().isEmpty()) {
            String keyword = "%" + request.getKeyword() + "%";
            criteria.andTitleLike(keyword);
        }

        PageHelper.startPage(request.getPage(), request.getSize());
        log.info("当前页码 current_page = {}", request.getPage());
        log.info("每页记录数 per_page = {}", request.getSize());

        List<Product> products = productMapper.selectByExample(productExample);

        PageInfo<Product> pageInfo = new PageInfo<>(products);
        log.info("总记录数 total = {}", pageInfo.getTotal());
        log.info("总页数 pages = {}", pageInfo.getPages());

        List<ProductResponse> list = BeanUtil.copyToList(products, ProductResponse.class);

        PageResponse<ProductResponse> pageResponse = new PageResponse<>();
        pageResponse.setTotal(pageInfo.getTotal());
        pageResponse.setList(list);

        return pageResponse;
    }
    public void save(ProductRequest request) {
        Product product = BeanUtil.copyProperties(request, Product.class);
        DateTime now = DateTime.now();
        if (ObjectUtil.isNull(product.getId()) || product.getId() == 0L) {
            product.setId(SnowflakeUtil.getId());
            product.setCreatedAt(now);
            product.setUpdatedAt(now);
            productMapper.insert(product);
        } else {
            product.setUpdatedAt(now);
            productMapper.updateByPrimaryKeySelective(product);
        }
    }

    public int deleteBatch(List<Long> ids) {
        ProductExample productExample = new ProductExample();
        ProductExample.Criteria criteria = productExample.createCriteria();
        criteria.andIdIn(ids);
        return productMapper.deleteByExample(productExample);
    }

    public ProductResponse getById(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        if (product == null) {
            return null;
        }
        return BeanUtil.copyProperties(product, ProductResponse.class);
    }

    public Product findById(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }
}
